<template>
  <custom-dialog
    ref="dialog" :title="modalTitle" height="500px"
    :before-confirm="beforeConfirm" :on-confirm="onConfirm"
  >
    <el-form ref="form" :model="info" :rules="rules" label-width="120px">
      <el-form-item label="企业名称" prop="enterpriseId">
        <el-select v-model="info.enterpriseId" class="width-100" @change="onEnterpriseChange">
          <el-option v-for="option in enterpriseOptions" :key="option.id" :label="option.enterpriseName" :value="option.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属地区" prop="areaId">
        <area-picker ref="areaPicker" v-model="info.areaId"></area-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="info.name"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="contactInformation">
        <el-input v-model="info.contactInformation" :maxlength="11"></el-input>
      </el-form-item>
      <el-form-item label="部门名称">
        <el-input v-model="info.departmentName"></el-input>
      </el-form-item>
      <el-form-item label="职务名称">
        <el-input v-model="info.positionName"></el-input>
      </el-form-item>
      <el-form-item label="专业职称">
        <el-input v-model="info.professionalTitle"></el-input>
      </el-form-item>
      <el-form-item label="备案状态">
        <el-switch v-model="info.filingStatus"></el-switch>
      </el-form-item>
    </el-form>
  </custom-dialog>
</template>

<script>
import CustomDialog from '@/components/custom-dialog'
import { saveOrUpdate } from '@/api/energyJobFiling'
import { getListEnterprise } from '@/api/enterprose'
import AreaPicker from '@/components/area-picker'
import REGEXP from '@/utils/regexp'

const api = {
  saveOrUpdate
}
export default {
  name: 'operate-modal',
  components: { AreaPicker, CustomDialog },
  props: {
    modalTitle: String,
    modalData: {
      type: Object,
      default () {
        return {}
      }
    },
  },
  data () {
    return {
      enterpriseOptions: [],
      info: {
        // 公司名称
        companyName: '',
        enterpriseId: '',
        // 类型
        type: '',
        // 姓名
        name: '',
        // 所属区域
        areaId: '',
        areaName: '',
        // 部门名称
        departmentName: '',
        // 职务名称
        positionName: '',
        // 联系方式
        contactInformation: '',
        // 专业职称
        professionalTitle: '',
        // 备案状态
        filingStatus: '0'
      },
      rules: {
        enterpriseId: [{ required: true, message: '必填项' }],
        areaId: [{ required: true, message: '必填项' }],
        name: [{ required: true, message: '必填项' }],
        contactInformation: [{
          required: true, message: '必填项',
        }, {
          validator (rule, value, callback) {
            if (REGEXP.PHONE_NUMBER.test(value)) {
              callback()
            } else {
              callback(new Error('请输入正确的手机号码'))
            }
          }
        }],
      },
    }
  },
  methods: {
    open () {
      this.info = {
        ...this.$options.data().info,
        ...this.modalData
      }
      this.info.filingStatus = this.info.filingStatus === '0'
      if (this.info.enterpriseId) {
        this.info.enterpriseId = Number(this.info.enterpriseId)
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.getListEnterprise()
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
    beforeConfirm () {
      return this.$refs.form.validate()
    },
    onEnterpriseChange () {
      const enterpriseId = this.info.enterpriseId
      const options = this.enterpriseOptions
      const target = options.find(v => v.id === enterpriseId)
      if (target) {
        this.info.companyName = target.enterpriseName
        this.info.areaId = target.districtBelong
      }
    },
    onConfirm (close) {
      const params = { ...this.info }
      params.filingStatus = params.filingStatus ? '0' : '1'
      params.areaName = this.$refs.areaPicker.getLabel()
      if (!params.id) {
        delete params.id
      }
      return api.saveOrUpdate(params).then(res => {
        if (res.code === 200) {
          this.$emit('save-success')
          this.$message.success('保存成功')
          close()
        }
      })
    },
    getListEnterprise () {
      getListEnterprise().then(res => {
        if (res.code === 200) {
          this.enterpriseOptions = res.data
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
